package com.my.aslife.controller;

import com.my.aslife.module.common.ErrorCode;
import com.my.aslife.module.common.ResponseResult;
import com.my.aslife.module.entity.User;
import com.my.aslife.module.entity.dtos.RegisterDto;
import com.my.aslife.module.entity.dtos.ResetDto;
import com.my.aslife.module.entity.dtos.UserLoginByEmailDto;
import com.my.aslife.module.entity.dtos.UserLoginDto;
import com.my.aslife.service.EmailService;
import com.my.aslife.service.IUserService;
import com.my.aslife.utils.MyUtils;
import com.my.aslife.utils.RegexUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Base64;

/**
 * 登录 登出
 * @Author mayuan
 */
@RestController
@RequestMapping("/api/my/login")
@Slf4j
@CrossOrigin
public class LogInAndOutController {

    @Autowired
    private IUserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private EmailService emailService;

    @PostMapping("/loginc")
    public ResponseResult login(@RequestBody UserLoginDto user) {

        if (user.getUserName() == null || user.getPassword() == null) {
            return ResponseResult.failed(ErrorCode.PARAMS_NOT_NULL);
        }

        return userService.login(user);
    }

    @PostMapping("/logine")
    public ResponseResult loginByEmailCode(@RequestBody UserLoginByEmailDto user) {

        if (user.getEmail() == null || user.getCode() == null) {
            return ResponseResult.failed(ErrorCode.PARAMS_NOT_NULL);
        }
        user.setEmail(new String(Base64.getDecoder().decode(user.getEmail())));

        return userService.loginByEmailCode(user);
    }

    @PostMapping("/register/zh")
    public ResponseResult registerByUserName(@RequestBody User user) {

        userService.registerByUserName(user);

        return ResponseResult.ok(null);
    }

    @PostMapping("/register/email")
    public ResponseResult registerByEmail(@RequestBody RegisterDto registerDto) {

        ErrorCode errorCode = MyUtils.checkRegisterDtoOrResetPwdDto(registerDto);
        if (errorCode != null) {
            return ResponseResult.failed(errorCode);
        }
        userService.registerByEmail(registerDto);
        UserLoginDto userLoginDto = new UserLoginDto();
        BeanUtils.copyProperties(registerDto.getUser(), userLoginDto);
        return userService.login(userLoginDto);
    }

    @PostMapping("/reset/pwd")
    public ResponseResult resetPwd(@RequestBody ResetDto resetDto){
        ErrorCode errorCode = MyUtils.checkRegisterDtoOrResetPwdDto(resetDto);
        if (errorCode != null) {
            return ResponseResult.failed(errorCode);
        }
        return userService.ResponseResult(resetDto);
    }

    @GetMapping("/send/register/email")
    public ResponseResult sendRegisterEmail(@RequestParam("to") String to) {
        // false 正确    true 错误
        boolean emailInvalid = RegexUtils.isEmailInvalid(to);
        if (emailInvalid) {
            return ResponseResult.failed(ErrorCode.EMAIL_ERROR);
        }
        return  emailService.sendRegisterEmail(to);
    }

    @GetMapping("/send/reset/email")
    public ResponseResult sendResetEmail(@RequestParam("to") String to) {
        // false 正确    true 错误
        boolean emailInvalid = RegexUtils.isEmailInvalid(to);
        if (emailInvalid) {
            return ResponseResult.failed(ErrorCode.EMAIL_ERROR);
        }
        return emailService.sendResetPwdEmail(to);
    }

    @GetMapping("/send/login/email")
    public ResponseResult sendLoginEmail(@RequestParam("to") String to) {
        // false 正确    true 错误
        boolean emailInvalid = RegexUtils.isEmailInvalid(to);
        if (emailInvalid) {
            return ResponseResult.failed(ErrorCode.EMAIL_ERROR);
        }
        return emailService.sendLoginEmail(to);
    }
}
